package com.graduation_design.modules.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.SysRole;
import com.graduation_design.entity.SysRoleMenu;
import com.graduation_design.modules.app.controller.BaseController;
import com.graduation_design.service.SysRoleMenuService;
import com.graduation_design.service.SysRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends BaseController {

    @Resource
    SysRoleService sysRoleService;

    @Resource
    SysRoleMenuService sysRoleMenuService;

    @GetMapping("/list")
    @RequiresPermissions("sys:role:list")
    public Result list(SysRole sysRole) {
        Page roles = sysRoleService.page(getPage(), new QueryWrapper<SysRole>()
                .like(StrUtil.isNotBlank(sysRole.getName()), "name", sysRole.getName())
                .like(StrUtil.isNotBlank(sysRole.getCode()), "code", sysRole.getCode())
                .orderByDesc("created")
        );
        return Result.success(roles);
    }

    /**
     * 除了查看角色基本信息，还查出了该角色对应的权限菜单的Id列表，为了编辑分配权限时候回显该角色对应的操作权限。
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:role:list")
    public Result info(@PathVariable Long id) {
        SysRole role = sysRoleService.getById(id);

        List<Long> menuIds = sysRoleMenuService.listObjs(new QueryWrapper<SysRoleMenu>()
                .eq("role_id", id).select("menu_id"),
                o -> Long.parseLong(o.toString())
        );

        role.setMenuIds(menuIds);
        return Result.success(role);
    }

    @PostMapping("/save")
    @RequiresPermissions(value = {"sys:user:save", "sys:user:update"}, logical = Logical.OR)
    public Result save(@Validated @RequestBody SysRole sysRole) {

        if (sysRole.getId() == null) {
            sysRole.setCreated(LocalDateTime.now());
        } else {
            sysRole.setUpdated(LocalDateTime.now());
        }

        sysRoleService.saveOrUpdate(sysRole);
        return Result.success(sysRole);
    }

    /**
     * 删除角色信息
     */
    @PostMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public Result delete(@RequestBody Long[] ids) {

        sysRoleService.deleteBath(ids);
        return Result.success();
    }

    /**
     * 给角色分配菜单权限
     */
    @PostMapping("/perm/{roleId}")
    @RequiresPermissions("sys:role:perm")
    public Result perm(@PathVariable Long roleId, @RequestBody Long[] menuIds) {

        sysRoleService.perm(roleId, menuIds);
        return Result.success();
    }
}
